package controllers

import (
	channel2 "github.com/flaravel/quanlaolao/app/models/channel"
	cps2 "github.com/flaravel/quanlaolao/app/models/cps"
	"github.com/flaravel/quanlaolao/pkg/config"
	"github.com/flaravel/quanlaolao/pkg/types"
	"github.com/syyongx/php2go"
	"net/http"
)

type IndexController struct {
	BaseController
}

func (c IndexController) Index(w http.ResponseWriter, r *http.Request)  {
	w.Header().Set("Content-Type", "text/json; charset=utf-8")

	query :=  r.URL.Query()
	channelId := query.Get("cate_id")

	cps,_:= cps2.GetList(types.StringToInt(channelId))
	channels,_:= channel2.GetList()

	var channelInterface []interface{}

	for _, channel := range channels {
		php2go.ArrayUnshift(&channelInterface,channel)
	}

	php2go.ArrayUnshift(&channelInterface,channel2.Channel{
		Name:      "全部",
		Logo:      config.GetString("app.url") + "/upload/channel/a181a603769c1f98ad927e7367c7aa51.png",
		Sort:      0,
	})

	data := make(map[string]interface{})
	data["category"] = channelInterface
	data["projects"] = cps
	data["is_jump"] = config.GetString("app.is_jump")

	c.Success(w,data)
}